package com.andy.eqm.server.socket;

import com.andy.eqm.server.dto.AddBagMessage;
import com.andy.eqm.server.dto.ConfsMessage;
import com.andy.eqm.server.dto.UserMessage;
import com.andy.eqm.server.game.GameUser;
import com.andy.eqm.server.game.Global;
import com.andy.eqm.server.socket.codec.IWrite;

public class SendUtil {

	public static void send100(MySocketSession session,GameUser gameUser) {
		UserMessage um = new UserMessage(gameUser);
		doSend(session,GmAction.ACTION_100, um);
	}
	
	public static void send300(MySocketSession session, int version) {
		ConfsMessage um = new ConfsMessage(version);
		doSend(session,GmAction.ACTION_300, um);
	}
	
	public static void send110(MySocketSession ses,Integer... itemId) {
		AddBagMessage um = new AddBagMessage(ses.getUser(),itemId);
		doSend(ses,GmAction.ACTION_110, um);
	}
	
	/**
	 * 发送信息
	 * @param sessionOrId 可以是session，gameUser,uid
	 * @param action
	 * @param data
	 */
	private static void doSend(Object sessionOrId,int action,IWrite data) {
		if(data == null)return;
		if(sessionOrId != null){
			MySocketSession sess = null;
			if(sessionOrId instanceof MySocketSession){
				sess = (MySocketSession) sessionOrId;
			}else if(sessionOrId instanceof GameUser){
				sess = Global.getSession(((GameUser) sessionOrId).getId());
			}else{
				sess = Global.getSession((Long) sessionOrId);
			}
			if(sess != null){
				data.setCmd(action);
				sess.sendMessage(data);
			}
		}
	}
	
	public static void broadcast(int action,IWrite data) {
		if(data == null)return;
		for(MySocketSession session:Global.getUserSessions()){
			doSend(session,action,data);
		}
	}


}
